﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WHC.Hotel.BLL;
using WHC.Pager.Entity;

using WHC.Hotel.Entity;
using WHC.OrderWater.Commons;

namespace WHC.Hotel.UI
{
    public partial class FrmBookRoomManager : BaseForm
    {
        public FrmBookRoomManager()
        {
            InitializeComponent();
        }

        private void FrmBookRoomManager_Load(object sender, EventArgs e)
        {

            txtBeginTime.Value = DateTime.Now.Date;
            txtEndTime.Value = DateTime.Now.Date;
            cmTimeType.SelectedIndex = 0;
            this.winGridViewPager1.DisplayColumns = "BookNo,RoomType,RoomNo,RoomCount,CustomerName,CustomerTel,BookPeople,BookPeoplePhone,PreArrivalTime,PreOffTime,CustomerCompany,Source,status";
            this.winGridViewPager1.dataGridView1.DataBindingComplete += new DataGridViewBindingCompleteEventHandler(dataGridView1_DataBindingComplete);
            this.winGridViewPager1.ProgressBar = this.toolStripProgressBar1.ProgressBar;
            this.winGridViewPager1.OnPageChanged += new EventHandler(winGridViewPager1_OnPageChanged);
            this.winGridViewPager1.OnStartExport += new EventHandler(winGridViewPager1_OnStartExport);
            // this.winGridViewPager1.OnEditSelected += new EventHandler(winGridViewPager1_OnEditSelected);
            // this.winGridViewPager1.OnAddNew += new EventHandler(winGridViewPager1_OnAddNew);

            this.winGridViewPager1.dataGridView1.Columns.Add("status", "状态");
            // this.winGridViewPager1.OnRefresh += new EventHandler(winGridViewPager1_OnRefresh);
            this.winGridViewPager1.OnDeleteSelected += new EventHandler(winGridViewPager1_OnDeleteSelected);
            BindData();

            //column = new DataGridViewTextBoxColumn();
            //column.HeaderText = "设备状态";
            //column.Width = 80;
            //column.DisplayIndex = 8;
            //column.Name = "ShowStatus";
            //column.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            //dgView.Columns.Add(column);
            
            this.winGridViewPager1.dataGridView1.Columns["Note"].Visible = false;
            this.winGridViewPager1.dataGridView1.Columns["ID"].Visible = false;
            this.winGridViewPager1.dataGridView1.Columns["IsFixRoomNo"].Visible = false;
            this.winGridViewPager1.dataGridView1.Columns["RetentionTime"].Visible = false;
            //this.winGridViewPager1.dataGridView1.Columns["BookPeople"].Visible = false;
           // this.winGridViewPager1.dataGridView1.Columns["BookPeoplePhone"].Visible = false;
            //this.winGridViewPager1.dataGridView1.Columns.Add("status", "状态");



          
            this.winGridViewPager1.AppendedMenu = this.contextMenuStrip1;
           // winGridViewPager1.auto
        }

        void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            foreach (DataGridViewRow row in winGridViewPager1.dataGridView1.Rows)
            {

                if (row.Cells["BookStatus"].Value.ToString() == "0")
                {

                    row.Cells["status"].Value = "正常";


                }
                else if (row.Cells["BookStatus"].Value.ToString() == "1")
                {

                    row.Cells["status"].Value = "取消";
                }
                else
                {

                    row.Cells["status"].Value = "入住";
                
                }

               



            }

        }

        private void winGridViewPager1_OnRefresh(object sender, EventArgs e)
        {
            BindData();
        }

        private void winGridViewPager1_OnDeleteSelected(object sender, EventArgs e)
        {
            if (MessageUtil.ShowYesNoAndTips("您确定删除选定的记录么？") == DialogResult.No)
            {
                return;
            }

            DataGridView grid = this.winGridViewPager1.dataGridView1;
            if (grid != null)
            {
                foreach (DataGridViewRow row in grid.SelectedRows)
                {
                                                       
                    BLLFactory<Book>.Instance.Delete(row.Cells["ID"].Value.ToString());

                }
                BindData();
            }
        }

        private void winGridViewPager1_OnEditSelected(object sender, EventArgs e)
        {
            DataGridView grid = this.winGridViewPager1.dataGridView1;
            if (grid != null)
            {
                foreach (DataGridViewRow row in grid.SelectedRows)
                {
                    string bookId = row.Cells["ID"].Value.ToString();
                    BookInfo bookInfo = BLLFactory<Book>.Instance.FindByID(bookId);
                    if (bookInfo != null)
                    {
                        FrmBookRoom dlg = new FrmBookRoom();
                        dlg.BookInfo = bookInfo;
                        if (DialogResult.OK == dlg.ShowDialog())
                        {
                            BindData();
                        }
                    }

                    break;
                }
            }
        }

        private void winGridViewPager1_OnAddNew(object sender, EventArgs e)
        {
            FrmBookRoom dlg = new FrmBookRoom();
            dlg.ShowDialog();
            BindData();
        }

        private void winGridViewPager1_OnStartExport(object sender, EventArgs e)
        {
            PagerInfo pagerInfo = new PagerInfo();
            pagerInfo.CurrenetPageIndex = 1;
            pagerInfo.PageSize = int.MaxValue;
            this.winGridViewPager1.AllToExport = BLLFactory<Book>.Instance.GetAllToDataSet(pagerInfo).Tables[0];//product.GetAllToDataSet(pagerInfo).Tables[0];
        }

        private void winGridViewPager1_OnPageChanged(object sender, EventArgs e)
        {
            BindData();
        }

        private void BindData()
        {

            SetColumnAlias();
            SearchCondition condition = new SearchCondition();
            condition.AddCondition("RoomNo", this.txtName.Text, SqlOperator.Like)
                .AddCondition("CustomerName", this.txtName.Text, SqlOperator.Like)
                .AddCondition("CustomerTel", this.txtName.Text, SqlOperator.Like);
            if (cmTimeType.Text == "预抵时间")
                 {
                  condition.AddCondition("PreArrivalTime", txtBeginTime.Text, SqlOperator.MoreThanOrEqual);
                  condition.AddCondition("PreArrivalTime", txtEndTime.Text + " 23:59:00", SqlOperator.LessThanOrEqual);

                 }else

                 {
                    condition.AddCondition("PreOffTime", txtBeginTime.Text, SqlOperator.MoreThanOrEqual);
                    condition.AddCondition("PreOffTime", txtEndTime.Text + " 23:59:00", SqlOperator.LessThanOrEqual);

                 }

            condition.AddCondition("IsTeam", 0, SqlOperator.Equal);
            string where = condition.BuildConditionSql(DatabaseType.SqlServer).Replace("Where", "");
            List<BookInfo> list = BLLFactory<Book>.Instance.Find(where, this.winGridViewPager1.PagerInfo);
            this.winGridViewPager1.DataSource = new WHC.Pager.WinControl.SortableBindingList<BookInfo>(list);

            this.winGridViewPager1.dataGridView1.Refresh();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            BindData();
        }

        private void txtName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnSearch_Click(null, null);
            }
        }

        private void menu_Cancel_Click(object sender, EventArgs e)
        {
            DataGridView dg = this.winGridViewPager1.dataGridView1;
            if (dg.SelectedRows.Count > 0)
            {
                foreach (DataGridViewRow row in dg.SelectedRows)
                {
                    if (row.Cells["ID"].Value != null)
                    {
                        int id = Convert.ToInt32(row.Cells["ID"].Value.ToString());
                        int  status = Convert.ToInt32(row.Cells["BookStatus"].Value);
                        if (status==1||status==2)
                        {
                            return;
                        }
                        BLLFactory<Book>.Instance.UpdateBookIn(id, 1);
                        //BLLFactory<Room>.Instance.SetRoomStatus(row.Cells["RoomNo"].Value.ToString(), RoomStatus.空净房.ToString());
                        MessageUtil.ShowTips("预定取消成功！");
                        BindData();
                    }
                    break;
                }
            }            
        }

        private void menu_Order_Click(object sender, EventArgs e)
        {
            DataGridView dg = this.winGridViewPager1.dataGridView1;
            if (dg.SelectedRows.Count > 0)
            {
                foreach (DataGridViewRow row in dg.SelectedRows)
                {
                    if (row.Cells["ID"].Value != null)
                    {

                        int status = Convert.ToInt32(row.Cells["BookStatus"].Value);
                        if (status==1)
                        {

                            MessageUtil.ShowTips("预定信息已经取消了，不能进行客房开单");
                            return;
                        
                        }

                        BookInfo bookInfo = BLLFactory<Book>.Instance.FindByID(Convert.ToInt32(row.Cells["ID"].Value.ToString()));
                        FrmEditRoomOrder dlgOrder = new FrmEditRoomOrder();
                        RoomInfo roomInfo=new RoomInfo();
                        if (!bookInfo.IsFixRoomNo)//未确定房间
                        {


                            FrmShowCanUseRoomm dlg = new FrmShowCanUseRoomm();
                            dlg.bookInfo = bookInfo;
                            if (dlg.ShowDialog() == DialogResult.OK)
                            {

                                roomInfo.RoomType = bookInfo.RoomType;
                                dlgOrder.RoomInfo = dlg.roomInfo;
                                dlgOrder.txtCompanyName.Text = bookInfo.CustomerCompany;
                                dlgOrder.txtName.Text = bookInfo.CustomerName;
                                dlgOrder.txtSource.Text = bookInfo.Source;
                                dlgOrder.txtTelephone.Text = bookInfo.CustomerTel;
                                dlgOrder.txtPreOff.Text = bookInfo.PreOffTime.ToString("yyyy-MM-dd")+" 14:00";
                                if (dlgOrder.ShowDialog() == DialogResult.OK)
                                {
                                    //删除预订
                                    BLLFactory<Book>.Instance.UpdateBookIn(Convert.ToInt32(bookInfo.ID.ToString()), 2);
                                }

                            }

                        }
                        else
                        {

                            string roomNo = row.Cells["RoomNo"].Value.ToString();
                            roomInfo = BLLFactory<Room>.Instance.FindByRoomNo(roomNo);

                            if (roomInfo.Status != RoomStatus.空净房 || roomInfo.Status != RoomStatus.空脏房)
                            {
                                MessageUtil.ShowTips("该房间（" + roomNo + "）处于非可用状态，不能入住");
                                return;
                            
                            }
                            
                            dlgOrder.RoomInfo = roomInfo;
                            dlgOrder.txtCompanyName.Text = bookInfo.CustomerCompany;
                            dlgOrder.txtName.Text = bookInfo.CustomerName;
                            dlgOrder.txtSource.Text = bookInfo.Source;
                            dlgOrder.txtTelephone.Text = bookInfo.CustomerTel;
                            dlgOrder.txtPreOff.Text = bookInfo.PreOffTime.ToString("yyyy-MM-dd") + "14:00";
                            if (dlgOrder.ShowDialog() == DialogResult.OK)
                            {
                                //更新预定状态
                               BLLFactory<Book>.Instance.UpdateBookIn(Convert.ToInt32(bookInfo.ID.ToString()),2);
                            }
                        
                        
                        }
                        
                      
                    }
                    break;
                }
            }          
        }

        /// <summary>
        /// 设置列别名
        /// </summary>
        private void SetColumnAlias()
        {

            #region 添加别名解析
            //this.winGridViewPager1.AddColumnAlias("ID", "编号");
            this.winGridViewPager1.AddColumnAlias("BookNo", "预订单号");
            this.winGridViewPager1.AddColumnAlias("RoomType", "房间类型");
            this.winGridViewPager1.AddColumnAlias("RoomNo", "房间号");
            this.winGridViewPager1.AddColumnAlias("RoomCount", "房间数");
            this.winGridViewPager1.AddColumnAlias("CustomerName", "宾客姓名");
            this.winGridViewPager1.AddColumnAlias("CustomerTel", "联系电话");
            this.winGridViewPager1.AddColumnAlias("BookPeople", "预定人");
            this.winGridViewPager1.AddColumnAlias("BookPeoplePhone", "联系电话");
            this.winGridViewPager1.AddColumnAlias("PreArrivalTime", "预到达时间");
            this.winGridViewPager1.AddColumnAlias("PreOffTime", "预离开时间");

            this.winGridViewPager1.AddColumnAlias("CustomerCompany", "公司名称");
            this.winGridViewPager1.AddColumnAlias("Source", "宾客来源");

            this.winGridViewPager1.AddColumnAlias("Note", "备注");
            this.winGridViewPager1.AddColumnAlias("BookStatus", "状态");
            #endregion
        
        
        
        }

        private void btnSearchToday_Click(object sender, EventArgs e)
        {



            SetColumnAlias();
            SearchCondition condition = new SearchCondition();
            condition.AddCondition("PreArrivalTime", System.DateTime.Now.ToString("yyyy-MM-dd"), SqlOperator.MoreThanOrEqual);
            condition.AddCondition("PreArrivalTime", System.DateTime.Now.ToString("yyyy-MM-dd") + " 23:59:00", SqlOperator.LessThanOrEqual);
            condition.AddCondition("IsTeam", 0, SqlOperator.Equal);

            string where = condition.BuildConditionSql(DatabaseType.SqlServer).Replace("Where", "");
            List<BookInfo> list = BLLFactory<Book>.Instance.Find(where, this.winGridViewPager1.PagerInfo);
            this.winGridViewPager1.DataSource = new WHC.Pager.WinControl.SortableBindingList<BookInfo>(list);

            this.winGridViewPager1.dataGridView1.Refresh();

        }

        private void BookRoomMenuItem_Click(object sender, EventArgs e)
        {


            FrmBookRoomNoFixNum dlg = new FrmBookRoomNoFixNum();
            dlg.ShowDialog();
            BindData();
        }
    }
}
